home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Ghost 1.0 / source / Ghost ƒ / Ghost code / ghost dictionary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.1 KB  |  186 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        ghost dictionary.c
  4.  
  5. Purpose:    This module handles dictionary maintenance -- loading the
  6.             words from disk and making sure the dictionaries haven't
  7.             been tampered with.
  8.  
  9.  
  10. Ghost -=- a classic word-building challenge
  11. Copyright (C) 1993 Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "ghost globals.h"
  31. #include "ghost dictionary.h"
  32. #include "ghost files.h"
  33. #include "msg environment.h"
  34.  
  35. Ptr                gTheDictionary[2];
  36. long            gIndex[2][27];
  37. unsigned char    gUseFullDictionary;
  38.  
  39. static int        partialRefNum;
  40. static int        fullRefNum;
  41.  
  42. void DisposeDictionary(void)
  43. {
  44.     if (gTheDictionary[kPartial])
  45.     {
  46.         DisposePtr(gTheDictionary[kPartial]);
  47.         gTheDictionary[kPartial]=0L;
  48.     }
  49.     
  50.     if (gTheDictionary[kFull])
  51.     {
  52.         DisposePtr(gTheDictionary[kFull]);
  53.         gTheDictionary[kFull]=0L;
  54.     }
  55. }
  56.  
  57. int LoadDictionary(void)
  58. {
  59.     long            thisDirID;
  60.     OSErr            isHuman;
  61.     long            thisEOF;
  62.     FSSpec            fs;
  63.     int                i;
  64.     Handle            tempHandle;
  65.     long            count;
  66.     
  67.     tempHandle=GetResource('Indx', 128);
  68.     if (tempHandle==0L)
  69.         LoadResource(tempHandle);
  70.     if (tempHandle==0L)
  71.         return kNoIndex;
  72.     HLock(tempHandle);
  73.     BlockMove(*tempHandle, &(gIndex[0][0]), 216);    
  74.     ReleaseResource(tempHandle);
  75.     
  76.     thisDirID=GetApplicationParID();
  77.     if (thisDirID==-1L)
  78.         return kNoDictionaries;
  79.     
  80.     partialRefNum=fullRefNum=0;
  81.     gTheDictionary[kPartial]=gTheDictionary[kFull]=0L;
  82.     
  83.     MyMakeFSSpec(0, thisDirID, "\p:Ghost dict (small)", &fs);
  84.     if (gHasFSSpecs)
  85.         isHuman=FSpOpenDF(&fs, fsRdPerm, &partialRefNum);
  86.     else
  87.         isHuman=HOpen(0, thisDirID, fs.name, fsRdPerm, &partialRefNum);
  88.     
  89.     if (isHuman!=noErr)
  90.         return kCantFindSmallDict;
  91.     
  92.     GetEOF(partialRefNum, &thisEOF);
  93.     if (thisEOF!=gIndex[kPartial][26])
  94.     {
  95.         CloseDictionary();
  96.         return kSmallDictDamaged;
  97.     }
  98.         
  99.     gTheDictionary[kPartial]=NewPtr(thisEOF);
  100.     if (gTheDictionary==0L)
  101.     {
  102.         CloseDictionary();
  103.         return kNoMemory;
  104.     }
  105.     
  106.     count=thisEOF;
  107.     SetFPos(partialRefNum, 1, 0L);
  108.     isHuman=FSRead(partialRefNum, &count, gTheDictionary[kPartial]);
  109.     CloseDictionary();
  110.     if (isHuman!=noErr)
  111.         return kCantReadSmallDict;
  112.     
  113.     
  114.     MyMakeFSSpec(0, thisDirID, "\p:Ghost dict (large)", &fs);
  115.     if (gHasFSSpecs)
  116.         isHuman=FSpOpenDF(&fs, fsRdPerm, &fullRefNum);
  117.     else
  118.         isHuman=HOpen(0, thisDirID, fs.name, fsRdPerm, &fullRefNum);
  119.     
  120.     if (isHuman!=noErr)
  121.         return kCantFindLargeDict;
  122.     
  123.     GetEOF(fullRefNum, &thisEOF);
  124.     if (thisEOF!=gIndex[kFull][26])
  125.     {
  126.         CloseDictionary();
  127.         return kLargeDictDamaged;
  128.     }
  129.         
  130.     gTheDictionary[kFull]=NewPtr(thisEOF);
  131.     if (gTheDictionary==0L)
  132.     {
  133.         CloseDictionary();
  134.         return kNoMemory;
  135.     }
  136.     
  137.     count=thisEOF;
  138.     SetFPos(fullRefNum, 1, 0L);
  139.     isHuman=FSRead(fullRefNum, &count, gTheDictionary[kFull]);
  140.     CloseDictionary();
  141.     if (isHuman!=noErr)
  142.         return kCantReadLargeDict;
  143.  
  144.     for (i=0; i<26; i++)
  145.     {
  146.         if (*((char*)((long)gTheDictionary[kPartial]+gIndex[kPartial][i]))!=('A'+i))
  147.             return kSmallDictDamaged;
  148.         if (*((char*)((long)gTheDictionary[kFull]+gIndex[kFull][i]))!=('A'+i))
  149.             return kLargeDictDamaged;
  150.     }
  151.     
  152.     return allsWell;
  153. }
  154.  
  155. void CloseDictionary(void)
  156. {
  157.     if (partialRefNum)
  158.     {
  159.         FSClose(partialRefNum);
  160.         partialRefNum=0;
  161.     }
  162.     
  163.     if (fullRefNum)
  164.     {
  165.         FSClose(fullRefNum);
  166.         fullRefNum=0;
  167.     }
  168. }
  169.  
  170. long GetApplicationParID(void)
  171. {
  172.     CInfoPBRec        pb;
  173.     int                err;
  174.     
  175.     pb.hFileInfo.ioCompletion=0L;
  176.     pb.hFileInfo.ioNamePtr=CurApName;
  177.     pb.hFileInfo.ioVRefNum=0;
  178.     pb.hFileInfo.ioFDirIndex=0;
  179.     pb.hFileInfo.ioDirID=0;
  180.     err=PBGetCatInfo(&pb, FALSE);
  181.     if (err!=noErr)
  182.         return -1L;
  183.     
  184.     return pb.hFileInfo.ioFlParID;
  185. }
  186.